package com.tanhua.server.controller;

import com.tanhua.mongo.Movement;
import com.tanhua.mongo.Visitors;
import com.tanhua.myenum.CommentType;
import com.tanhua.server.service.MovementService;
import com.tanhua.vo.MovementsVo;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.VisitorsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/movements")
public class MovementController {
    @Autowired
    private MovementService movementService;

    /**
     * 发布动态
     * @param movement
     * @param multipartFiles
     * @return
     * @throws IOException
     */
    @PostMapping
    public ResponseEntity publish(Movement movement, MultipartFile[] multipartFiles) throws IOException {

        movementService.publish(movement,multipartFiles);
        return ResponseEntity.ok(null);
    }

    /**
     * 查看我的动态
     * @param userId
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                       @RequestParam(defaultValue = "1")Integer page,
                                       @RequestParam(defaultValue = "10")Integer pagesize){

         PageResult result = movementService.findByUserId(userId,page,pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 查询好友动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity findFriendMovement(@RequestParam(defaultValue = "1")Integer page,
                                       @RequestParam(defaultValue = "10")Integer pagesize){

        PageResult result = movementService.findFriendMovement(page,pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 推荐动态
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1")Integer page,
                                       @RequestParam(defaultValue = "10")Integer pagesize){

        PageResult result = movementService.recommend(page,pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 查看单条动态
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String id){
        log.info("查看单条动态执行了");
        if("visitors".equals(id)){
            return ResponseEntity.ok(null);
        }
        MovementsVo result = movementService.findById(id);
        return ResponseEntity.ok(result);
    }

    /**
     * 点赞，喜欢或取消点赞，取消喜欢方法
     *
     * @param movementId
     * @param type
     * @return
     */
    @GetMapping("/{id}/{type}")
    public ResponseEntity likeOrLove(@PathVariable("id") String movementId,
                                     @PathVariable("type") String type) {
        //记录操作类型
        CommentType commentType = null;
        //记录正向反向操作
        boolean flag = true;
        if ("like".equals(type)) {
            commentType = CommentType.LIKE;
        } else if ("dislike".equals(type)) {
            commentType = CommentType.LIKE;
            flag = false;
        } else if ("love".equals(type)) {
            commentType = CommentType.LOVE;
        } else if ("unlove".equals(type)) {
            commentType = CommentType.LOVE;
            flag = false;
        } else {

        }
        Integer CountNumber = movementService.likeOrLove(movementId, commentType, flag);

        return ResponseEntity.ok(CountNumber);
    }

    /**
     * 谁看过我接口
     * @return
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
       List<VisitorsVo> list = movementService.visitors();
        return ResponseEntity.ok(list);
    }

}
